/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import model.HoSo;
import model.PhuCap;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.HibernateUtil;

/**
 *
 * @author MRDO
 */
public class GrantPhuCapToHoSo {
    private static Session session;
    
    public GrantPhuCapToHoSo(){
        SessionFactory factory = HibernateUtil.getSessionFactory();
        this.session = factory.getCurrentSession();
    }
    
    /*
     * grantPhuCapToHoSo
     */
    public boolean grantPhuCapToHoSo(String maNV,String maPC){
        try {
            session.beginTransaction();
            
            PhuCap phuCap = (PhuCap)session.get(PhuCap.class, maPC);
            HoSo hoSo = (HoSo) session.get(HoSo.class, maNV);
            
            phuCap.getHoSos().add(hoSo);
            hoSo.getPhuCaps().add(phuCap);
            
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        }
        return false;
    }

    
}
